package com.topisv.tms.service.lightShelf;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.EdiOrderItem;
import com.topisv.tms.entity.User;
import com.topisv.tms.entity.lightShelf.LightShelf;
import com.topisv.tms.entity.lightShelf.LightShelfStock;
import com.topisv.tms.entity.lightShelf.LightShelfStockSku;
import com.topisv.tms.entity.log.OrderLog;
import com.topisv.tms.entity.work.WorkBatch;
import com.topisv.tms.entity.work.WorkLog;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.entity.work.WorkTask;
import com.topisv.tms.entity.work.WorkTaskItem;
import com.topisv.tms.entity.workBox.WorkBox;
import com.topisv.tms.repository.AreaGroupDao;
import com.topisv.tms.repository.CustomersDao;
import com.topisv.tms.repository.EdiOrderDao;
import com.topisv.tms.repository.UserDao;
import com.topisv.tms.repository.lightShelf.LightShelfDao;
import com.topisv.tms.repository.lightShelf.LightShelfStockDao;
import com.topisv.tms.repository.lightShelf.LightShelfStockSkuDao;
import com.topisv.tms.repository.log.OrderLogDao;
import com.topisv.tms.repository.work.WorkBatchDao;
import com.topisv.tms.repository.work.WorkLogDao;
import com.topisv.tms.repository.work.WorkOrderDao;
import com.topisv.tms.repository.work.WorkTaskDao;
import com.topisv.tms.repository.work.WorkTaskItemDao;
import com.topisv.tms.repository.workBox.WorkBoxDao;
import com.topisv.tms.rest.qimen.qmsend.QmRequestSender;
import com.topisv.tms.rest.qimen.qmservice.DeliveryProcessService;
import com.topisv.tms.service.work.WorkOrderService;
import com.topisv.tms.service.work.WorkTaskService;

/**
 * 灯光架_service
 * 
 * @author ch
 * 
 */
@Component
@Transactional(readOnly = true)
public class LightShelfService {
	private static final Logger log = LoggerFactory.getLogger(LightShelfService.class);
	
	@Autowired
	private LightShelfDao lightShelfDao;
	@Autowired
	private CustomersDao customersDao;
	@Autowired
	private AreaGroupDao areaGroupDao;
	@Autowired
	private LightShelfStockDao lightShelfStockDao;
	@Autowired
	private LightShelfStockSkuDao lightShelfStockSkuDao;
	@Autowired
	private WorkTaskDao workTaskDao;
	@Autowired
	private WorkOrderService workOrderService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private WorkLogDao workLogDao;
	@Autowired
	private WorkTaskService workTaskService;
	@Autowired
	private WorkOrderDao workOrderDao;
	@Autowired
	private OrderLogDao orderLogDao;
	@Autowired
	private WorkBatchDao workBatchDao;
	@Autowired
	private WorkBoxDao workBoxDao;
	@Autowired
	private WorkTaskItemDao workTaskItemDao;
	@Autowired
	private EdiOrderDao ediOrderDao;
	
	@Resource(type=DeliveryProcessService.class)
	private QmRequestSender<Map<String,Object>> deliveryProcessService;
	

	/**
	 * @param warehouseCode 仓库编码
	 * @param status 灯光架状态
	 * @return
	 */
	public List<LightShelf> loadLightShelf(String warehouseCode, String status) {
		return this.lightShelfDao.loadLightShelf(warehouseCode,status);
	}

	/**
	 * 查找灯光架
	 * @param param
	 * @return
	 */
	public List<LightShelf> loadLightShelfByParam(Map<String, Object> param) {
		Specification<LightShelf> spec = getWhereClause(param);
		List<LightShelf> lightShelfList= this.lightShelfDao.findAll(spec);
		for (LightShelf lightShelf : lightShelfList) {
			lightShelf.setCustomer(this.customersDao.findByCustomerCode(lightShelf.getCustomerCode()));
			lightShelf.setWarehouse(this.areaGroupDao.findByGroupCode(lightShelf.getWarehouseCode()));
		}
		return lightShelfList;
	}
	
	/**
	 * 查询条件封装
	 * @param param
	 * @return
	 */
	private Specification<LightShelf> getWhereClause(final Map<String, Object> param) {
		
		return new Specification<LightShelf>(){
			
		    @Override
		    public Predicate toPredicate(Root<LightShelf> r, CriteriaQuery<?> q, CriteriaBuilder cb) {
		      Predicate pre = cb.conjunction();
		      
		      String customerCode=(String) param.get("customerCode");
		      if(StringUtils.isNotEmpty(customerCode)){
					pre.getExpressions().add(cb.equal(r.get("customerCode"),customerCode));
			  }

		      String warehouseCode=(String) param.get("warehouseCode");
		      if(StringUtils.isNotEmpty(warehouseCode)){
		    	  pre.getExpressions().add(cb.equal(r.get("warehouseCode"),warehouseCode));
		      }
		      
		      return pre;
		    }
		  };
       }

	/**
	 * 创建灯光架
	 * @param lightShelf 灯光架
	 * @throws Exception 
	 */
	@Transactional(readOnly = false, rollbackFor=Exception.class)
	public void save(LightShelf lightShelf) throws Exception {
		try {
			
			Integer stockNum=lightShelf.getStockNum();
			lightShelf.setStatus("free");

			List<LightShelfStock> stockList=new ArrayList<LightShelfStock>();
//			for (int i = 1; i <=stockNum; i++) {
//				LightShelfStock stock=new LightShelfStock();
//				stock.setLightShelf(lightShelf);
//				stock.setStatus("free");
//				stock.setStockCode(i+"");
//				stock.setStockName("第"+i+"号");
//				stockList.add(stock);
//			}
			
			int tmp_index = 1;
			if("1".equals(lightShelf.getShelfType()))
			{
				String[] code = lightShelf.getBoardInfo().split(",");
				for(int i=0 ; i<stockNum; i++)
				{
					LightShelfStock stock=new LightShelfStock();
					stock.setStatus("free");
					stock.setLightShelf(lightShelf);
					int index = i+1;
					String index_str = "";
					if(index < 10)
					{
						index_str = "0" + index;
					}
					else
					{
						index_str = "" + index;
					}
					if(index <=30)
					{
						index_str = code[0] +index_str;
						stock.setBoardCode(code[0]);
					}
					if(index <=60 && index > 30)
					{
						int tmsIndex = index-30;
						if(tmsIndex < 10)
						{
							index_str = "0" + tmsIndex;
						}
						else
						{
							index_str = "" + tmsIndex;
						}
						index_str = code[1] +index_str;
						stock.setBoardCode(code[1]);
					}
					if(index <=90 && index > 60)
					{
						int tmsIndex = index-60;
						if(tmsIndex < 10)
						{
							index_str = "0" + tmsIndex;
						}
						else
						{
							index_str = "" + tmsIndex;
						}
						index_str = code[2] +index_str;
						stock.setBoardCode(code[2]);
					}
					stock.setStockCode(index+"");
					stock.setStockName(index_str);
					stockList.add(stock);
				}
			}
			else if("2".equals(lightShelf.getShelfType()))
			{
				String[] code = lightShelf.getBoardInfo().split(",");
				for(int i=0 ; i<stockNum; i++)
				{
					LightShelfStock stock=new LightShelfStock();
					stock.setStatus("free");
					stock.setLightShelf(lightShelf);
					int index = i+1;
					String index_str = "";
					if(index < 10)
					{
						index_str = "0" + index;
					}
					else
					{
						index_str = "" + index;
					}
					if(index <=60)
					{
						index_str = code[0] +index_str;
						stock.setBoardCode(code[0]);
					}
					if(index <=120 && index > 60)
					{
						int tmsIndex = index-60;
						if(tmsIndex < 10)
						{
							index_str = "0" + tmsIndex;
						}
						else
						{
							index_str = "" + tmsIndex;
						}
						index_str = code[1] +index_str;
						stock.setBoardCode(code[1]);
					}
					if(index <=180 && index > 120)
					{
						int tmsIndex = index-120;
						if(tmsIndex < 10)
						{
							index_str = "0" + tmsIndex;
						}
						else
						{
							index_str = "" + tmsIndex;
						}
						index_str = code[2] +index_str;
						stock.setBoardCode(code[2]);
					}
					stock.setStockCode(index+"");
					stock.setStockName(index_str);
					stockList.add(stock);
				}
			}
			else if("3".equals(lightShelf.getShelfType()))
			{
				String[] code = lightShelf.getBoardInfo().split(",");
				for(int i=0 ; i<stockNum; i++)
				{
					LightShelfStock stock=new LightShelfStock();
					stock.setStatus("free");
					stock.setLightShelf(lightShelf);
					int index = i+1;
					String index_str = "";
					if(index < 10)
					{
						index_str = "0" + index;
					}
					else
					{
						index_str = "" + index;
					}
					if(index <=60)
					{
						if(index%2 == 0)
						{
							continue;
						}
						index_str = code[0] +index_str;
						stock.setBoardCode(code[0]);
					}
					if(index <=120 && index > 60)
					{
						int tmsIndex = index-60;
						if(tmsIndex%2 == 0)
						{
							continue;
						}
						if(tmsIndex < 10)
						{
							index_str = "0" + tmsIndex;
						}
						else
						{
							index_str = "" + tmsIndex;
						}
						index_str = code[1] +index_str;
						stock.setBoardCode(code[1]);
					}
					if(index <=180 && index > 120)
					{
						int tmsIndex = index-120;
						if(tmsIndex%2 == 0)
						{
							continue;
						}
						if(tmsIndex < 10)
						{
							index_str = "0" + tmsIndex;
						}
						else
						{
							index_str = "" + tmsIndex;
						}
						index_str = code[2] +index_str;
						stock.setBoardCode(code[2]);
					}
					stock.setStockCode(tmp_index+"");
					stock.setStockName(index_str);
					stockList.add(stock);
					tmp_index ++ ;
				}
			}
			
			this.lightShelfStockDao.save(stockList);
			this.lightShelfDao.save(lightShelf);
		
		} catch (Exception e) {
			throw new Exception(e);
		}
		
	}
	
	/**
	 * 删除灯光架
	 * @param lightShelfId 灯光架ID
	 * @throws Exception 
	 */
	@Transactional(readOnly = false, rollbackFor=Exception.class)
	public void delete(Long lightShelfId) throws Exception {
		try {
			LightShelf lightShelf=this.lightShelfDao.findOne(lightShelfId);
			List<LightShelfStock> stockList=this.lightShelfStockDao.loadStock(lightShelf.getId());
			this.lightShelfStockDao.delete(stockList);
			this.lightShelfDao.delete(lightShelf);
		} catch (Exception e) {
			throw new Exception(e);
		}
		
	}

	/**
	 * 查询灯光架
	 * @param lightShelfId 灯光架Id
	 * @return
	 */
	public LightShelf loadLightShelfById(Long lightShelfId) {
		return this.lightShelfDao.findOne(lightShelfId);
	}

	/**
	 * 商品上架
	 * @param shelfId 灯光架ID
	 * @param sku  sku 
	 * @param taskId 任务ID
	 * @return  hasfinished:任务已经完成    error:异常   success:上架验证通过
	 * @throws Exception 
	 */
	@Transactional(readOnly = false, rollbackFor=Exception.class)
	public Map<String,String> setGoods(String shelfCode, String sku, String taskCode ) throws Exception {
		
		try {
			Map<String,String> result=new HashMap<String, String>();
			
			WorkTask task=this.workTaskDao.loadTaskByTaskCode(taskCode);
			LightShelf shelf=this.lightShelfDao.loadLightShelfByShelfCode(shelfCode);
			List<LightShelfStock> stockList=null;
			
			stockList=this.lightShelfStockDao.loadByShelfId(shelf.getId());
			
			if(!taskCode.equals(shelf.getTaskCode())){
				result.put("msg", "error"); //架编号和任务编号不匹配
				return result;
			}
				
			//任务已经完成
			List<LightShelfStockSku> allStockSkuList=this.lightShelfStockSkuDao.loadUnSet(shelf.getId(),task.getTaskCode());
			if(allStockSkuList!=null && allStockSkuList.size()==0){
				result.put("msg", "hasfinished");
				return result;
			}
			
			//确定放置架位
			LightShelfStock sel_stock=null;
			boolean flag=false;
			for (LightShelfStock _stock : stockList) {
				List<LightShelfStockSku> _stockSkuList=this.lightShelfStockSkuDao.loadByStockId(_stock.getId());
				for (LightShelfStockSku _stockSku : _stockSkuList) {
					if(_stockSku.getSku().equals(sku) && _stockSku.getUpStatus().equals("0")){
						_stockSku.setUpStatus("1");
						flag=true;
						break;
					}
				}
				if(flag==true){
					sel_stock=_stock;
					break;
				}
			}
			
			//验证上架通过
			if(sel_stock!=null){
				result.put("msg", "success");
				result.put("stockCode",sel_stock.getStockCode());
				result.put("stockName",sel_stock.getStockName());
				//验证商品是否全部入位
				List<LightShelfStockSku> unSetList=this.lightShelfStockSkuDao.loadUnSet(task.getLightshelfId(),task.getTaskCode());
				if(unSetList!=null && unSetList.size()==0){
					result.put("isFinished", "finished"); //全部入位
				}else{
					result.put("isFinished", "notfinished"); //没有全部入位
				}
				return result;
			}
			
			result.put("msg", "error");
			return result;
			
			
		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	/**
	 * 完成灯光拣选任务
	 * @param taskId 任务id 
	 * @param jobuuid 
	 * @return
	 * @throws Exception 
	 */
	@Transactional(readOnly = false, rollbackFor=Exception.class)
	public String completeLampPickTask(String taskCode) throws Exception {
		try {
			
			WorkTask task=this.workTaskDao.loadTaskByTaskCode(taskCode);
			Date now=new Date();
			User user=this.userDao.findOne(task.getLamperId());
			
			//修改任务状态
			task.setStatus("5"); //任务状态( 1.创建任务 2.已分配拣货任务  3.已拣货  4. 已分配灯光拣选任务  5.已经灯光拣选  6.已分配包装任务  7.已包装  8.已分配发货任务  9.已发货  10.灯光拣选进行中..)
			this.workTaskDao.save(task);
			
			//添加作业日志
			WorkLog workLog=new WorkLog(
					task.getBatchCode(),
					"6", //操作类型 (1.创建批次 2.创建任务 3.领取拣货任务  4.完成拣货任务 5.领取灯光拣选任务 6.完成灯光拣选任务  7.领取包装任务 8完成包装任务  9.领取发货任务  10.完成发货任务   )
					user.getRealname()+"完成灯光拣选任务（"+task.getTaskCode()+"）", 
					now, 
					user);
			this.workLogDao.save(workLog);
			
			
			//修改库存状态：从订单占用状态减去
//			this.workTaskService.updateStock(task);
			
			//修改订单状态，添加日志
			List<WorkOrder> orderList=this.workOrderDao.loadOrderByTaskCode(task.getTaskCode());
			List<OrderLog> logList=new ArrayList<OrderLog>();
			for (WorkOrder order : orderList) {
				
				OrderLog log=new OrderLog(
						order.getOrder().getOrdeno(),
						task.getCustomerCode(),
						"8", //操作类型 （1.创建批次 2.创建任务  3.拣货完成 4.包装完成 5.发货完成 6.签收完成 7.异常登记 8.完成灯光拣选任务 9.打印单据）
						user,
						now,
						null,
						user.getRealname()+"完成灯光拣选任务");
				logList.add(log);
				
				order.setStatus("9"); //订单状态 (1.创建批次 2.创建任务 3.已拣货  4.已灯光拣选  9.待包装   5.已包装  6.已发货  7.已签收  8.异常 )
			}
			
			this.workOrderDao.save(orderList);
			this.orderLogDao.save(logList);
			
			//查看同一批次的其他任务是否完成，如果都完成，修改入库批次状态为：3
			List<WorkTaskItem> uncompletedTaskList=this.workTaskItemDao.loadUnPickItemByBatchCode(task.getBatchCode());
			if(uncompletedTaskList==null || uncompletedTaskList.size()==0){
				WorkBatch batch=this.workBatchDao.loadbatchByBatchCode(task.getBatchCode());
				batch.setStatus("3"); //批次状态 (1.已创建批次 2.已创建任务 3.已拣货  4.已配单 5.已包装  6.已发货)
				this.workBatchDao.save(batch);
				
				//添加工作日志
				WorkLog workLog1=new WorkLog(
						task.getBatchCode(),
						"3",
						"拣货任务全部完成", 
						now, 
						null);
				this.workLogDao.save(workLog1);
			}
			
			//清空灯光架信息
			Long lightShelfId=task.getLightshelfId();
			LightShelf shelf=this.lightShelfDao.findOne(lightShelfId);
			shelf.setStatus("free");
			shelf.setBatchCode(null);
			shelf.setTaskCode(null);
			this.lightShelfDao.save(shelf);
			
			List<LightShelfStock> stockList=this.lightShelfStockDao.loadStock(shelf.getId());
			for (LightShelfStock stock : stockList) {
				stock.setGoodsInfo(null);
				stock.setGoodsNum(null);
				stock.setOrderno(null);
				stock.setBatchCode(null);
				stock.setTaskCode(null);
				stock.setStatus("free");
			}
			this.lightShelfStockDao.save(stockList);
			
			List<LightShelfStockSku> stockSkuList=this.lightShelfStockSkuDao.loadByShelfId(shelf.getId());
			this.lightShelfStockSkuDao.delete(stockSkuList);
				for(WorkOrder wo:orderList) {
					try{
						log.info("-----发送奇门出库单流水信息 已灯光拣货 orderno"+wo.getOrder().getOrdeno());
						Map<String,Object> params = new HashMap<String,Object>();
						EdiOrder ediOrder = ediOrderDao.findByOrdeno(wo.getOrder().getOrdeno());
					    params.put("ediOrder", ediOrder);
					    params.put("status", "pick");
					    deliveryProcessService.send(params,ediOrder.getCustomerCode());
						log.info("-----发送奇门出库单流水信息发送完毕 已灯光拣货 end");
					}catch(Exception e){
						log.error("-----发送奇门出库单流水信息发送完毕 灯光拣货 error");
					}
				}
			return "success";
			
		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	public LightShelf loadLightShelfByShelfCode(String shelfCode) {
		return this.lightShelfDao.loadLightShelfByShelfCode(shelfCode);
	}

	/**
	 * 任务绑定灯光架
	 * @param shelfCode
	 * @param taskCode
	 * @return busy：使用中   success:成功绑定
	 * @throws Exception 
	 */
	@Transactional(readOnly = false, rollbackFor=Exception.class)
	public Map<String, String> bindLightShelf(String shelfCode, String boxCode,String jobuuid)  {

			Map<String, String> map=new HashMap<String, String>();
			
			LightShelf shelf=this.lightShelfDao.loadLightShelfByShelfCode(shelfCode);
			if(shelf==null){
				map.put("msg", "shelfNotFound");
				log.error("have not found this shelf:"+shelfCode+" boxCode:" +boxCode+" jobid:"+jobuuid);
				return map;
			}
			WorkBox box=this.workBoxDao.loadByBoxCode(boxCode,shelf.getWarehouseCode());
			if(box==null){
				map.put("msg", "boxNotFound");
				log.error(shelf.getWarehouseCode()+ " have not found this box:"+boxCode);
				return map;
			}
			if(null == box.getTaskCode()||"".equals(box.getTaskCode().trim())){
				map.put("msg", "boxNotInTask");
				log.error(" box:"+boxCode +" not in task.");
				return map;
			}
			WorkTask task=this.workTaskDao.loadTaskByTaskCode(box.getTaskCode());
			if(task==null){
				map.put("msg", "taskNotFound");
				log.error(" have not found task, taskCode:"+box.getTaskCode()+"  from box:"+boxCode);
				return map;
			}
			String taskCode=box.getTaskCode();
			
			List<LightShelfStock> stockList=null;
			List<LightShelfStockSku> stockSkuList=null;
			String batchCode=task.getBatchCode();
			
			if("10".equals(task.getStatus())){
				map.put("msg", "continue");
				map.put("taskCode", task.getTaskCode());
				return map;
			}
			if(!("3".equals(task.getStatus())||"4".equals(task.getStatus())))
			{
				map.put("msg", "error");
				return map;
			}
			
			//灯光架被占用
			if(shelf.getStatus().equals("busy")){
				map.put("msg", "busy");
				return map;
			}
			
			//灯光架数位不够此任务订单数
			int orderQty=task.getOrderQty();
			int stockQty=shelf.getStockNum();
			if(stockQty<orderQty){
				map.put("msg", "less");
				return map;
			}
			
			//订单信息
			Map<String, Object> param=new HashMap<String, Object>();
		    param.put("taskCode",taskCode);
		    List<WorkOrder> orderList=this.workOrderService.loadOrder(param);
			
		    //绑定灯光架
			shelf.setStatus("busy");
			shelf.setBatchCode(batchCode);
			shelf.setTaskCode(taskCode);
			this.lightShelfDao.save(shelf);
			
			//绑定灯光架位、写入灯光架明细信息
			stockList=new ArrayList<LightShelfStock>();
			stockSkuList=new ArrayList<LightShelfStockSku>();
			for (WorkOrder workOrder : orderList) {
				
				int rankno=workOrder.getRankno();
				LightShelfStock stock=this.lightShelfStockDao.loadStockByCondition(shelf.getId(),rankno+"");
				
				int goodsNum=0; String goodsInfo="";
	            List<EdiOrderItem> itemList=workOrder.getOrder().getOrderItems();
				for (EdiOrderItem orderItem : itemList) {
					int _qty=orderItem.getQty();
					String _sku=orderItem.getSku();
					goodsNum+=_qty;
	                goodsInfo+=_sku+"_"+_qty+";";
	                for(int i=0;i<_qty;i++){
	                	LightShelfStockSku stockSku=new LightShelfStockSku();
	                	stockSku.setLightShelf(shelf);
	                	stockSku.setStock(stock);
	                	stockSku.setSku(_sku);
	                	stockSku.setNum(1);
	                	stockSku.setUpStatus("0");
	                	stockSku.setBatchCode(batchCode);
	                	stockSku.setTaskCode(taskCode);
	                	stockSkuList.add(stockSku);
	                }
				}
				
				stock.setStatus("busy");
				stock.setGoodsInfo(goodsInfo);
				stock.setGoodsNum(goodsNum);
				stock.setBatchCode(batchCode);
				stock.setTaskCode(taskCode);
				stock.setOrderno(workOrder.getOrder().getOrdeno());
				stockList.add(stock);
			}
			
			this.lightShelfStockDao.save(stockList);
			this.lightShelfStockSkuDao.save(stockSkuList);
			
			//设置任务状态为: 灯光拣选进行中
			task.setStatus("10"); //任务状态( 1.创建任务 2.已分配拣货任务  3.已拣货  4. 已分配灯光拣选任务  5.已经灯光拣选  6.已分配包装任务  7.已包装  8.已分配发货任务  9.已发货  10.灯光拣选进行中..)
			task.setLightshelfId(shelf.getId());
			task.setLightshelfCode(shelf.getShelfCode());
			User user=this.userDao.findByJobuuid(jobuuid);
			task.setLamperId(user.getId());
			this.workTaskDao.save(task);
			
			map.put("msg", "success");
			map.put("taskCode", task.getTaskCode());
			return map;

	}
	
	/**
//	 * 商品上灯光架_手机
//	 * @param shelfId 灯光架ID
//	 * @param sku  sku 
//	 * @param taskId 任务ID
//	 * @return busy:灯光架使用中   hasfinished:任务已经完成    error:异常   success:上架验证通过
//	 * @throws Exception 
//	 */
//	@Transactional(readOnly = false, rollbackFor=Exception.class)
//	public Map<String,String> setGoods_phone(String shelfCode, String sku, String taskCode) throws Exception {
//		
//		try {
//			Map<String,String> result=new HashMap<String, String>();
//			
//			WorkTask task=this.workTaskDao.loadTaskByTaskCode(taskCode);
//			LightShelf shelf=this.lightShelfDao.loadLightShelfByShelfCode(shelfCode);
//			
//			//验证选择的灯光架是否被其它任务占用
//			if(!shelf.getTaskCode().equals(taskCode)){
//				result.put("msg", "error");
//			}
//			
//			//订单信息
//			Map<String, Object> param=new HashMap<String, Object>();
//		    param.put("taskCode",taskCode);
//		    List<LightShelfStock> stockList=this.lightShelfStockDao.loadByShelfId(shelf.getId());
//			
//				
//			//任务已经完成
//			List<LightShelfStockSku> allStockSkuList=this.lightShelfStockSkuDao.loadUnSet(shelf.getId(),task.getTaskCode());
//			if(allStockSkuList!=null && allStockSkuList.size()==0){
//				result.put("msg", "hasfinished");
//			}
//			
//			//确定放置架位
//			LightShelfStock sel_stock=null;
//			boolean flag=false;
//			for (LightShelfStock _stock : stockList) {
//				List<LightShelfStockSku> _stockSkuList=this.lightShelfStockSkuDao.loadByStockId(_stock.getId());
//				for (LightShelfStockSku _stockSku : _stockSkuList) {
//					if(_stockSku.getSku().equals(sku) && _stockSku.getUpStatus().equals("0")){
//						_stockSku.setUpStatus("1");
//						flag=true;
//						break;
//					}
//				}
//				if(flag==true){
//					sel_stock=_stock;
//					break;
//				}
//			}
//			
//			//验证上架通过
//			if(sel_stock!=null){
//				result.put("msg", "success");
//				result.put("stockCode",sel_stock.getStockCode());
//				//验证商品是否全部入位
//				List<LightShelfStockSku> unSetList=this.lightShelfStockSkuDao.loadUnSet(task.getLightshelfId(),task.getTaskCode());
//				if(unSetList!=null && unSetList.size()==0){
//					result.put("isFinished", "finished"); //全部入位
//				}else{
//					result.put("isFinished", "notfinished"); //没有全部入位
//				}
//			}
//			
//			return result;
//			
//		} catch (Exception e) {
//			
//			throw new Exception(e.getMessage());
//		}
//		
//	}

	/**
	 * 释放
	 * @param lightShelfId
	 * @throws Exception 
	 */
	@Transactional(readOnly = false, rollbackFor=Exception.class)
	public void free(Long lightShelfId) throws Exception {
		
		try {
			
			LightShelf shelf=this.lightShelfDao.findOne(lightShelfId);
			shelf.setBatchCode(null);
			shelf.setTaskCode(null);
			shelf.setStatus("free");
			this.lightShelfDao.save(shelf);
			
			List<LightShelfStock> stockList=this.lightShelfStockDao.loadByShelfId(lightShelfId);
			for (LightShelfStock stock : stockList) {
				stock.setBatchCode(null);
				stock.setTaskCode(null);
				stock.setStatus("free");
				stock.setGoodsInfo(null);
				stock.setGoodsNum(0);
				stock.setOrderno(null);
				stock.setUpStatus(null);
			}
			this.lightShelfStockDao.save(stockList);
			
			List<LightShelfStockSku> stockSkuList=this.lightShelfStockSkuDao.loadByShelfId(lightShelfId);
			this.lightShelfStockSkuDao.delete(stockSkuList);
			
		} catch (Exception e) {
			throw e;
		}
		
		
	}



}